#include"SList.h"
SListNode* SListNodeBuy(SListTypeData x)
{
	SListNode* node = (SListNode*)malloc(sizeof(SListNode));
	if (node == NULL)
	{
		printf("malloc fail\n");
		return NULL;
	}
	else
	{
		node->data = x;
		node->next = NULL;
	}
	return node;
}
void SListPrint(SListNode* plist)
{
	
		while (plist != NULL)
		{
			printf("%d->", plist->data);
			plist = plist->next;
		}
		printf("NULL");
		return;
}
void SListPushBack(SListNode** pplist,SListTypeData x)
{
	SListNode* newnode = SListNodeBuy(x);
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		SListNode* tail = *pplist;
		while (tail->next != NULL)
		{
			tail=tail->next;
		}
		tail->next = newnode;
	}
}
void SListPopBack(SListNode** pplist)
{
	SListNode* prev = NULL;
	SListNode* tail = *pplist;
	if (tail == NULL || tail->next == NULL)
	{
		free(tail);
		tail == NULL;
	}
	while (tail->next != NULL)
	{
		prev = tail;
		tail = tail->next;
	}
	free(tail);
	tail = NULL;
	prev->next = NULL;
}
void SListPushFront(SListNode** pplist,SListTypeData x)
{
	assert(pplist);
	SListNode* newnode = SListNodeBuy(x);
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		newnode->next = *pplist;
		*pplist = newnode;
	}
}
void SListPopFront(SListNode** pplist)
{
	assert(pplist);
	SListNode* first = *pplist;
	if (first == NULL)
	{
		return;
	}
	else
	{
		*pplist = first->next;
		free(first);
		first = NULL;
	}
}
SListNode* SListFind(SListNode** pplist, SListTypeData x)
{
	SListNode* cur = *pplist;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
void SListInsertAfter(SListNode*pos, SListTypeData x)
{
	assert(pos);
	SListNode* newnode = SListNodeBuy(x);
	SListNode* after = pos->next;
	pos->next = newnode;
	newnode->next = after;
}
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	SListNode* next = pos->next;
	if (next != NULL)
	{
		SListNode* nextnext = next->next;
		free(next);
		pos->next = nextnext;
	}
}